import cv2
import numpy as np
from PIL import Image
import os

def process_image(image_path):
    """
    处理上传的眼底图像
    
    Args:
        image_path: 图像文件路径
        
    Returns:
        处理后的图像数据
    """
    # 检查文件是否存在
    if not os.path.exists(image_path):
        raise FileNotFoundError(f"Image file not found: {image_path}")
    
    # 读取图像
    image = cv2.imread(image_path)
    if image is None:
        raise ValueError(f"Failed to read image: {image_path}")
    
    # 转换为RGB（OpenCV默认是BGR）
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    
    # 调整大小为标准尺寸（假设模型需要224x224的输入）
    image_resized = cv2.resize(image_rgb, (224, 224))
    
    # 这里可以添加更多的预处理步骤，如:
    # - 对比度增强
    # - 噪声去除
    # - 标准化
    
    # 标准化像素值到[0,1]
    image_normalized = image_resized / 255.0
    
    return {
        'original_path': image_path,
        'processed_data': image_normalized,
        'shape': image_normalized.shape
    } 